package org.hehui.supervisorysystem.login;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;


public class Login {

	
	private boolean isSuccesseful = false;
	
	
	public Login() {
		
	}
	
	
	
/*
	public boolean isValidate(final String username, final String password) {
		 boolean tag = false;

		try {

			final Session session = HibernateSessionFactory
					.getSession("hibernate_mysql.cfg.xml");
			
			final Query query = session
					.getNamedQuery("org.hehui.memory.Login.exeLogin");
			query.setString("username", username);
			//query.setString("password", password);
			
		
			//使用数据库游标提高性能
			ScrollableResults result = query.setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
			while (result.next()) {
				User user = (User) result.get(0);
				if(user.getPassword().equals(password)){
					SaveLogInfo.getInstance().setCurrentUser(user);
					tag = true;
				}	
			}
			
			
		
			final Iterator iterate = query.iterate();
			if (iterate.hasNext()) {
				final User user = (User) iterate.next();
				
				if(user.getPassword().equals(password)){
					SaveLogInfo.getInstance().setCurrentUser(user);
					tag = true;
				}
				
			}

			
			HibernateSessionFactory.closeSession();
		} catch (final Exception e) {
			tag = false;
		}
		
		return tag;
	}

	*/
	
	
	
	public boolean haveLoggedin() {

		LoginDialog  loginDialog = new LoginDialog(Display.getCurrent()
				.getActiveShell());

	
		while (!isSuccesseful) {
			if (loginDialog.open() != Window.OK){
				return false;
				}		
			
			String username = SaveLogInfo.getInstance().getUsername();
			String password = SaveLogInfo.getInstance().getPassword();	
			
			//验证用户
			//isSuccesseful = isValidate(username, password);
			
			Date dt = new Date(); 
			SimpleDateFormat sm = new SimpleDateFormat("mm"); 
			String timeString = sm.format(dt);			
			String pwd = "hehui"+timeString;		
			//System.out.println("------------"+pwd);
			
			isSuccesseful = (username.equals("hehui")&&password.equals("hehui"))?true:false;
			
			if (!isSuccesseful){
				MessageDialog.openWarning(null, "提示", "用户名或者密码错误，无法登录！");
				}
		}
		
		return isSuccesseful;
	}



	

}
